package com.netrust.cmsx.admin.modules.sys.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.netrust.cmsx.admin.modules.sys.common.AStatic;
import com.netrust.cmsx.admin.modules.sys.pojo.MenuPo;
import com.netrust.cmsx.admin.modules.sys.service.DictService;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.exception.ProvideException;
import com.netrust.cmsx.core.page.PageKey;
import com.netrust.cmsx.core.page.jq.JqGrid;
import com.netrust.cmsx.core.page.jq.JqPost;
import com.netrust.cmsx.core.page.jq.convert.Page2JQ;
import com.netrust.cmsx.core.persist.TreeEntityComp4Jq;
import com.netrust.cmsx.core.persist.TreeUtil;
import com.netrust.cmsx.core.resp.RespJson;
import com.netrust.cmsx.core.validator.BeanValidators;
import com.netrust.cmsx.core.web.BaseController;
import com.netrust.cmsx.entity.module.sys.Dict;
import com.netrust.cmsx.entity.module.sys.Menu;
import com.netrust.cmsx.entity.module.sys.Role;
import com.netrust.cmsx.utils.collections.CollectionUtils;
import com.netrust.cmsx.utils.data.DateUtils;
import com.netrust.cmsx.utils.excute.ExcuteTime;
import com.netrust.cmsx.utils.string.StringUtils;

import net.sf.json.JSONArray;


/**
 * 用户Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;
	
	@ResponseBody
	@RequestMapping(value = {""})
	public Dict get(@RequestParam(required=false) String id) {
		if (id!=null){
			ExcuteTime c=new ExcuteTime();
			Dict dict=dictService.findOne(id);
			c.new Balance().print("findById");
			return dict;
		}else{
			return new Dict();
		}
	}
	
	@RequestMapping(value = {"form"})
	public String form(@RequestParam(required=true) String id,String op,Model model) {
		Dict dict=new Dict();
		
		switch (op) {
			case "update":
				dict=dictService.findOne(id);
				break;
			default:
				dict.setPid(id);//设置父级对象
				break;
		}
		
		model.addAttribute("dict", dict);
		return "modules/sys/dictForm";
	}
	
	@RequestMapping(value = {"save"},method=RequestMethod.POST)
	public String save(Dict dict) {	
		try {
			dictService.saveDict(dict);
		} catch (ProvideException e) {
			e.printStackTrace();
		}
		return "modules/sys/dictList";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = {"del"})
	public RespJson del(@RequestParam(required=true) String id) {
		try {
			dictService.deleteInFlag(new Dict(id));
			return new RespJson(Global.Operate_Success, 200);
		} catch (Exception e) {
			return new RespJson(Global.Operate_Error, 500, null);
		}
	}
	
	@Deprecated
	@ResponseBody
	@RequestMapping(value = {"jqlist"})
	public JqGrid jqlist(JqPost jqPost) {
		PageKey pk=new PageKey(null, jqPost.getPage(),jqPost.getRows());
		Page<Dict> page =dictService.findAllList(pk);
		JqGrid jg=new Page2JQ<Dict>().run(page,new String[]{"loginName","email","phone","mobile","loginDate","remarks"});		
		return jg;
	}
	
	@RequestMapping(value = {"list"})
	@ResponseBody
	@RequiresPermissions("dict:view")
	public List<Dict> list() {
		List<Dict> list =dictService.findAllList();
		for (Dict dict : list) {
			dict.setExpanded(true);
			
			String[] pids=dict.getPids().split(",");
			
			dict.setLevel(pids.length-1);
		}
		
		List<Dict> list1=new TreeUtil<Dict>().getChildTree(list);
		
//		JSONArray jarr=JSONArray.fromObject(list1);
//		List<Dict> list2 = (List)jarr.toList(jarr, Dict.class);
//		
//		Collections.sort(list, new TreeEntityComp4Jq<Dict>());
		return list1;
	}
	
	@ResponseBody
	@RequestMapping(value = {"child"})
	public List<Dict> list(@RequestParam(required=false) String parentid,String nodeid,Integer n_level) {				
		parentid=parentid!=null?parentid:"0";
		nodeid=nodeid!=null?nodeid:"0";
		n_level=n_level!=null?n_level:0;
		
		List<Dict> list =dictService.findChild(new Dict(nodeid));
		
		for (Dict dict : list) {
			if(!dict.getPid().equals("0")){
				dict.setLevel(n_level+1);					
			}
		}
		
		return list;
	}
}
